package com.yu.scanner;

import com.yu.annotation.RpcScanner;
import com.yu.annotation.RpcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @PackageName: scanner
 * @Description:
 * @Version: v1.0.0
 * @Author: yuweilin
 * @Date: 13:13
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ------------------------------------------------------------
 * 2021/1/21      yuweilin         v1.0.0               新建
 */
@Slf4j
public class CustomBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
    private static final String SPRING_BEAN_BASE_PACKAGE = "com.yu";
    private static final String BASE_PACKAGE_ATTRIBUTE_NAME = "basePackage";
    private ResourceLoader resourceLoader;
    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }


    /**
     * 将beanDefinition注册到容器中
     * @param annotationMetadata
     * @param registry
     *
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
        String name = RpcScanner.class.getName();
        Map<String, Object> attributes = annotationMetadata.getAnnotationAttributes(name);
        //将从RpcScanner中获取的map进行一次封装，封装到AnnotationAttributes里面,其实直接取出来然后转换类型就行了
        AnnotationAttributes annotationAttributes = AnnotationAttributes.fromMap(attributes);
        String[] basePackages = new String[0];
        if (annotationAttributes!=null){
            basePackages = annotationAttributes.getStringArray(BASE_PACKAGE_ATTRIBUTE_NAME);
        }
        if (basePackages.length==0){
            //如果值没有进去，那么就使用注解元数据的的原来的类的包名放进去
            basePackages = new String[]{((StandardAnnotationMetadata) annotationMetadata).getIntrospectedClass().getPackage().getName()};
        }
        //创建两个扫描器，分别扫描component注解和rpcservice注解的类
        CustomBeanScanner springBeanScanner = new CustomBeanScanner(registry, Component.class);
        CustomBeanScanner rpcServiceScanner = new CustomBeanScanner(registry, RpcService.class);

        if (resourceLoader!=null){
            rpcServiceScanner.setResourceLoader(resourceLoader);
            springBeanScanner.setResourceLoader(resourceLoader);
        }

        int springBeanAmount = springBeanScanner.scan(SPRING_BEAN_BASE_PACKAGE);
        log.info("springBeanScanner扫描数量[{}]",springBeanAmount);
        int rpcServiceAmount = rpcServiceScanner.scan(basePackages);
        log.info("rpcServiceAmount扫描数量[{}]",rpcServiceAmount);

    }
}
